<h1>Promet po proizvodu</h1>
<?= $this->Form->create('Filter', array('class'=>'filter-form')) ?>

	<?= $this->Form->label('product_id', 'Proizvod:', array('class'=>'auto')) ?>
    <?= $this->element('product_picker', array(
	    'name'  => 'product_id',
	    'value' => @$filter['product_id'],
		'products' => $products
	)) ?>
	
	<?= $this->Form->label('date_start', 'Datum:', array('class'=>'inline')) ?>
	<?= $this->Form->text('date_start', array('class'=>'datepicker')) ?>
	<?= $this->Form->label('date_end', '-', array('class'=>'inline auto')) ?>
	<?= $this->Form->text('date_end', array('class'=>'datepicker')) ?>
	
	<?= $this->Form->submit('Pošalji', array('div'=>false)) ?>
<?= $this->Form->end() ?>

<? if(!empty($message)): ?>
<p class="clear result-message <?= @$message_class ?>"><?= $message ?></p>
<? endif; ?>

<? if(!empty($data)): ?>
<?
	$summary = array();
	$total = array('otprema'=>array('amount'=>0,'price'=>0), 'povraćaj'=>array('amount'=>0,'price'=>0));
?>
<table class="result-table">
	<tr>
		<th rowspan="2">Prodavnica</th>
		<th colspan="2" width="15%" align="center" class="otprema">Otpremljeno</th>
		<th colspan="3" width="15%" align="center" class="povracaj">Vraćeno</th>
		<th colspan="2" width="10%" align="center" class="subtotal">TOTAL</th>
	</tr>
	<tr>
		<th class="otprema">Kom.</th>
		<th align="right" class="otprema">Iznos</th>
		<th class="povracaj">Kom.</th>
		<th align="right" class="povracaj">Iznos</th>
		<th align="right" class="povracaj">%</th>
		<th class="subtotal">Kom.</th>
		<th class="subtotal" align="right">Iznos</th>
	</tr>
	<? foreach($data as $row): ?>
	<?
		$otprema_amount  = (int)$row['otprema']['amount'];
		$otprema_price   = (float)$row['otprema']['price'];
		$povracaj_amount = (int)$row['povraćaj']['amount'];
		$povracaj_price  = (float)$row['povraćaj']['price'];
		
		$total['otprema']['amount'] += $otprema_amount;
		$total['otprema']['price'] += $otprema_price;
		$total['povraćaj']['amount'] += $povracaj_amount;
		$total['povraćaj']['price'] += $povracaj_price;
	?>
	<tr>
		<td><?= $row['store']['name'] ?></td>
		<td align="center" class="otprema"><?= $otprema_amount ?></td>
		<td align="right" class="otprema"><?= currency_format($otprema_price) ?></td>
		<td align="center" class="povracaj"><?= $povracaj_amount ?></td>
		<td align="right" class="povracaj"><?= currency_format($povracaj_price) ?></td>
		<td align="right" class="povracaj"><?= $otprema_amount>0 ? round($povracaj_amount/$otprema_amount * 100,0) : 0 ?></td>
		<td class="subtotal"><?= $otprema_amount - $povracaj_amount ?></td>
		<td align="right" class="subtotal"><?= currency_format(($otprema_price)-($povracaj_price)) ?></td>
	</tr>
	<? endforeach; ?>
	<tr>
		<td></td>
		<td align="center" class="subtotal otprema"><?= (int)@$total['otprema']['amount'] ?></td>
		<td align="right" class="subtotal otprema"><?= currency_format(@$total['otprema']['price']) ?></td>
		<td align="center" class="subtotal povracaj"><?= (int)$total['povraćaj']['amount'] ?></td>
		<td align="right" class="subtotal povracaj"><?= currency_format(@$total['povraćaj']['price']) ?></td>
		<td align="right" class="subtotal povracaj"><?= $otprema_amount>0 ? round((int)$total['povraćaj']['amount']/(int)@$total['otprema']['amount']*100,2) : 0 ?>%</td>
		<td class="total"><?= (int)@$total['otprema']['amount'] - (int)@$total['povraćaj']['amount'] ?></td>
		<td align="right" class="total"><?= currency_format(@$total['otprema']['price'] - @$total['povraćaj']['price']) ?></td>
	</tr>
</table>
<? endif; ?>